public class num543 {
    //二叉树的直径
    int ans;//存放便利的直径的节点数
    public int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        order(root);//遍历root
        return ans - 1;
    }

    private int order(TreeNode root) {
        if(root == null){
            return 0;
        }
        int left = order(root.left);//遍历左子树
        int right = order(root.right);//遍历右子树
        ans = Math.max(ans,left + right + 1);//更新ans值
        return Math.max(left,right) + 1;
    }
}
